<!DOCTYPE HTML>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="HandheldFriendly" content="true" />
		<meta name="MobileOptimized" content="320" />

		<script type="text/javascript" src="js/common.js"></script>

		<script type="text/javascript">
			var shares = null,
				bhref = false;
			var Intent = null,
				File = null,
				Uri = null,
				main = null;
			// H5 plus事件处理
			function plusReady() {
				updateSerivces();
				if (plus.os.name == "Android") {
					Intent = plus.android.importClass("android.content.Intent");
					File = plus.android.importClass("java.io.File");
					Uri = plus.android.importClass("android.net.Uri");
					main = plus.android.runtimeMainActivity();
				}
			}
			if (window.plus) {
				plusReady();
			} else {
				document.addEventListener("plusready", plusReady, false);
			}
			/**
			 * 更新分享服务
			 */
			function updateSerivces() {
				plus.share.getServices(function(s) {
					shares = {};
					for (var i in s) {
						var t = s[i];
						shares[t.id] = t;
					}
				}, function(e) {
					outSet("获取分享服务列表失败：" + e.message);
				});
			}
			/**
			 * 调用系统分享
			 */
			function shareSystem() {
				if (plus.os.name !== "Android") {
					plus.nativeUI.alert("此平台暂不支持系统分享功能!");
					return;
				}
				var intent = new Intent(Intent.ACTION_SEND);
				var p = "";
				if (pic && pic.realUrl) {
					p = pic.realUrl;
					if (p.substr(0, 7) === "file://") {
						p = p.substr(7);
					} else if (p.sub(0) !== "/") {
						p = plus.io.convertLocalFileSystemURL(p);
					}
				}
				var f = new File(p);
				var uri = Uri.fromFile(f);
				if (f.exists() && f.isFile()) {
					console.log("image/*");
					intent.setType("image/*");
					intent.putExtra(Intent.EXTRA_STREAM, uri);
				} else {
					console.log("text/plain");
					intent.setType("text/plain");
				}
				intent.putExtra(Intent.EXTRA_SUBJECT, "HBuilder");
				intent.putExtra(Intent.EXTRA_TEXT, sharecontent.value);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				main.startActivity(Intent.createChooser(intent, "系统分享"));
			}
			/**
			 * 分享操作
			 */
			function shareAction(id, ex) {
				var s = null;
				outSet("分享操作：");
				if (!id || !(s = shares[id])) {
					outLine("无效的分享服务！");
					return;
				}
				if (s.authenticated) {
					outLine("---已授权---");
					shareMessage(s, ex);
				} else {
					outLine("---未授权---");
					s.authorize(function() {
						shareMessage(s, ex);
					}, function(e) {
						outLine("认证授权失败：" + e.code + " - " + e.message);
					});
				}
			}
			/**
			 * 发送分享消息
			 * @param {plus.share.ShareService} s
			 */
			function shareMessage(s, ex) {
				var msg = {
					content: sharecontent.value,
					extra: {
						scene: ex
					}
				};
				if (bhref) {
					msg.href = sharehref.value;
					if (sharehrefTitle && sharehrefTitle.value != "") {
						msg.title = sharehrefTitle.value;
					}
					if (sharehrefDes && sharehrefDes.value != "") {
						msg.content = sharehrefDes.value;
					}
					//分享图片
					msg.thumbs = ["_www/logo.png"];
					msg.pictures = ["_www/logo.png"];
				} else {
					if (pic && pic.realUrl) {
						msg.pictures = [pic.realUrl];
					}
				}
				outLine(JSON.stringify(msg));
				s.send(msg, function() {
					outLine("分享到\"" + s.description + "\"成功！ ");
				}, function(e) {
					outLine("分享到\"" + s.description + "\"失败: " + e.code + " - " + e.message);
				});
			}
			/**
			 * 解除所有分享服务的授权
			 */
			function cancelAuth() {
				try {
					outSet("解除授权：");
					for (var i in shares) {
						var s = shares[i];
						if (s.authenticated) {
							outLine("取消\"" + s.description + "\"");
						}
						s.forbid();
					}
					// 取消授权后需要更新服务列表
					updateSerivces();
					outLine("操作成功！");
				} catch (e) {
					alert(e);
				}
			}
			// 拍照添加图片分享
			function shareCameraPicture() {
				outSet("拍照添加分享图片：");
				var cmr = plus.camera.getCamera();
				cmr.captureImage(function(p) {
					plus.io.resolveLocalFileSystemURL(p, function(entry) {
						pic.src = entry.toLocalURL();
						pic.realUrl = p;
						outLine("拍照图片：" + pic.realUrl);
					}, function(e) {
						outLine("读取拍照文件错误：" + e.message);
					});
				}, function(e) {
					outLine("拍照失败：" + e.message);
				});
			}
			// 从相册添加图片分享
			function shareGalleryPicture() {
				outSet("从相册添加分享图片：");
				plus.gallery.pick(function(p) {
					// 从相册返回的路径不需要转换可以直接使用
					pic.src = p;
					pic.realUrl = pic.src;
					outLine("选择图片：" + pic.realUrl);
					//      plus.io.resolveLocalFileSystemURL(p,function(entry){
					//			pic.src=entry.toLocalURL();
					//			pic.realUrl=pic.src;
					//			outLine("选择图片："+pic.realUrl);
					//		},function(e){
					//			outLine("读取拍照文件错误："+e.message);
					//		} );
				});
			}
			// 使用Logo图片分享
			function shareLogoPicture() {
				outSet("使用Logo分享图片：");
				var url = "_www/logo.png";
				plus.io.resolveLocalFileSystemURL(url, function(entry) {
					pic.src = entry.toLocalURL();
					pic.realUrl = url;
				}, function(e) {
					outLine("读取Logo文件错误：" + e.message);
				});
			}
			// 打开分享
			function shareShow() {
				bhref = false;
				var ids = [{
						id: "weixin",
						ex: "WXSceneSession"
					}, {
						id: "weixin",
						ex: "WXSceneTimeline"
					}, {
						id: "sinaweibo"
					}, {
						id: "tencentweibo"
					}],
					bts = [{
						title: "发送给微信好友"
					}, {
						title: "分享到微信朋友圈"
					}, {
						title: "分享到新浪微博"
					}, {
						title: "分享到腾讯微博"
					}];
				if (plus.os.name == "iOS") {
					ids.push({
						id: "qq"
					});
					bts.push({
						title: "分享到QQ"
					});
				}
				plus.nativeUI.actionSheet({
						cancel: "取消",
						buttons: bts
					},
					function(e) {
						var i = e.index;
						if (i > 0) {
							shareAction(ids[i - 1].id, ids[i - 1].ex);
						}
					}
				);
			}
			// 分析链接
			function shareHref() {
				bhref = true;
				var ids = [{
						id: "weixin",
						ex: "WXSceneSession"
					}, {
						id: "weixin",
						ex: "WXSceneTimeline"
					}, {
						id: "qq"
					}],
					bts = [{
						title: "发送给微信好友"
					}, {
						title: "分享到微信朋友圈"
					}, {
						title: "分享到QQ"
					}];
				plus.nativeUI.actionSheet({
						cancel: "取消",
						buttons: bts
					},
					function(e) {
						var i = e.index;
						if (i > 0) {
							shareAction(ids[i - 1].id, ids[i - 1].ex);
						}
					}
				);
			}
		</script>
		<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
		<style type="text/css">
			#sharecontent {
				width: 80%;
				-webkit-user-select: text;
				border: 1px solid #6C6C6C;
				-webkit-border-radius: 2px;
				border-radius: 2px;
			}
			
			#pic {
				width: 100px;
				height: 100px;
				/*border: 1px dashed #CCCCCC;*/
			}
			
			.sharehref {
				width: 80%;
				-webkit-user-select: text;
				border: 1px solid #6C6C6C;
				-webkit-border-radius: 2px;
				border-radius: 2px;
				margin-bottom: .5em;
			}
		</style>
	</head>

	<body>
		<header id="header">
			<div class="nvtt">Share</div>
		</header>

		<div id="dcontent" class="dcontent">
			<br/>
			<p class="heading">分享内容：</p>
			<textarea id="sharecontent" rows="3">我正在使用HBuilder+HTML5开发移动应用，赶紧跟我一起来体验！</textarea>
			<br/>
			<br/>
			<p class="heading">分享图片：</p>
			<table style="width:100%;">
				<tbody>
					<tr>
						<td style="width:30%">
							<div class="button button-select" onclick="shareCameraPicture()">拍照</div>
						</td>
						<td style="width:30%">
							<div class="button button-select" onclick="shareGalleryPicture()">相册选取</div>
						</td>
						<td style="width:30%">
							<div class="button button-select" onclick="shareLogoPicture()">使用logo图</div>
						</td>
					</tr>
				</tbody>
			</table>
			<img id="pic" src="img/add.png" />
			<br/>
			<div class="button" onclick="shareShow()">普通分享(文字，图片)</div>
			<div class="button" onclick="shareSystem()">系统分享(IOS不支持)</div>

			<div style="padding: 0.5em 1em;">
				<hr color="#EEE" />
			</div>
			<p class="heading">链接地址：</p>
			<input id="sharehref" class="sharehref" type="url" value="http://www.dcloud.io/" placeholder="请输入要分享的链接地址" />
			<p class="heading">链接标题：</p>
			<input id="sharehrefTitle" class="sharehref" type="text" value="DCloud HBuilder-做最好的HTML5开发工具" placeholder="请输入要分享的链接标题" />
			<p class="heading">链接描述：</p>
			<input id="sharehrefDes" class="sharehref" type="text" value="我正在使用HBuilder+HTML5开发移动应用，赶紧跟我一起来体验！" placeholder="请输入要分享的链接描述" />
			<div class="button" onclick="shareHref()">分享链接</div>
			<br/>
			<br/>
			<div style="padding: 0.5em 1em;">
				<hr color="#EEE" />
			</div>
			<p class="des">如果需要解除分享中绑定的用户信息，请点击解除授权：</p>
			<div class="button" onclick="cancelAuth()">解除授权</div>
		</div>
		<div id="output">
			Share模块管理客户端的社交分享功能，提供调用终端社交软件的分享能力。通过plus.share可获取社交分享管理对象。
		</div>
	</body>
	<script type="text/javascript" src="js/immersed.js"></script>

</html>